package com.feiyi.app.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.feiyi.app.R;
import com.feiyi.app.adapter.AddressListAdapter;
import com.feiyi.app.app.AppConstant;
import com.feiyi.app.base.BaseActivity;
import com.feiyi.app.bean.AddressListBean;
import com.feiyi.app.event.AddressEvent;
import com.feiyi.app.event.ChooseAddressEvent;
import com.feiyi.app.net.HttpMethods;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody;
import okhttp3.RequestBody;

/**
 * @author wangliantao
 * @time 2018 11 2018/11/18 9:12 PM
 * @email 1346947404@qq.com
 * @package com.feiyi.app.activity
 * @describe： In me the tiger sniffs the rose
 */
public class MyAddressActivity extends BaseActivity implements OnRefreshListener {

    Unbinder unbinder;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

    private String uid;

    private String sid;

    private List<AddressListBean.DataBean> mList = new ArrayList<>();

    private AddressListAdapter mAdpter;

    private static final String TAG = "main";

    private String choosingAddress;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myaddress);
        unbinder = ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initView();
        initData();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThread(AddressEvent event){
        initData();
    }
    private void initView() {
        choosingAddress = getIntent().getStringExtra("choosingAddress");
        uid = spUtils.getString(AppConstant.UID);
        sid = spUtils.getString(AppConstant.SID);
        refreshlayout.setOnRefreshListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mAdpter = new AddressListAdapter(R.layout.item_address_list,mList);
        recyclerView.setAdapter(mAdpter);
        mAdpter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.iv_edit_address){
                    Bundle bundle = new Bundle();
                    bundle.putString("id",mList.get(position).getId());
                    ActivityUtils.startActivity(bundle,activity,CreateNewAddressActivity.class);
                }
            }
        });
        if (null != choosingAddress){
            recyclerView.addOnItemTouchListener(new OnItemClickListener() {
                @Override
                public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                    EventBus.getDefault().post(new ChooseAddressEvent(mList.get(position)));
                    finish();
                }
            });
        }
    }

    private void initData() {

        FormBody.Builder builder = new FormBody.Builder();
        builder.add("uid",uid);
        builder.add("sid",sid);
        RequestBody body = builder.build();
        HttpMethods.getInstance().info.getAddressList(AppConstant.ADDRESS_LIST_URL,body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AddressListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(AddressListBean addressListBean) {
                        mList.clear();
                        if (addressListBean.getData() != null && !addressListBean.getData().isEmpty()) {
                        mList.addAll(addressListBean.getData());
                        }else {
                            ToastUtils.showShort("用户地址为空");

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: "+e.toString() );
                        ToastUtils.showShort("获取地址列表失败");
                    }

                    @Override
                    public void onComplete() {
                        mAdpter.notifyDataSetChanged();
                    }
                });

    }

    @OnClick({R.id.iv_back_titlebar,R.id.but_add})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_back_titlebar:
                finish();
                break;
            case R.id.but_add:
                ActivityUtils.startActivity(CreateNewAddressActivity.class);
                break;
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        initData();
        refreshLayout.finishRefresh(3000);
    }
}
